// ==UserScript==
// @name         知乎沉浸式阅读
// @namespace    http://tampermonkey.net/
// @version      2.7
// @description  知乎沉浸式阅读 更加护眼 更加简洁 免登录 去除登录弹窗
// @author       white
// @match        https://zhuanlan.zhihu.com/*
// @match        https://www.zhihu.com/*
// @grant        none
// @icon         
// @run-at       document-start
// @license      MIT
// ==/UserScript==

const ZhiHu = {
  article: "article",
  answer: "answer",
};
(function () {
  "use strict";
  class Wonderful {
    constructor() {
      this.backgroundColor = "#557F43";
      this.width = "1000px";
      this.margin = "0 auto";
      // 需要拦截的黑名单事件
      this.winBlackEvenName = ["scroll", "wheel"];
      this.docBlackEvenName = ["scroll"];
      this.log = false;
      this.host = location.host;

      this.domTreeChange = [
        // 知乎文章
        () => {
          if (this.answer() == ZhiHu.article) {
            document.querySelector(".Post-Row-Content-right")?.remove();
            document.querySelector(".AppHeader")?.parentElement.remove();
            // 底部状态栏关闭
            document.querySelector("article > div:nth-child(5)")?.remove();
            // 关闭底部文章推荐
            document.querySelector("div.Post-Sub.Post-NormalSub")?.remove();
          }
        },
        // 知乎问答
        () => {
          if (this.answer() == ZhiHu.answer) {
            // 移除侧边
            document.querySelector(".Question-sideColumn")?.remove();
            // 取消宽度限制
            let ele = document.querySelector(".Question-mainColumn");
            if (ele) {
              ele.style.width = "unset";
            }
            // 主体内容
            ele = document.querySelector(".Question-main");
            if (ele) {
              ele.style.width = this.width;
              ele.style.padding = "unset";
              ele.style.margin = this.margin;
            }
            // 关注信息
            ele = document.querySelector(
              "div.QuestionHeader-content div.Question-main"
            );
            if (ele) {
              ele.style.width = "unset";
            }
            // 文章信息
            ele = document.querySelector("div.QuestionHeader");
            if (ele) {
              ele.style.borderRadius = "5px";
              ele.style.minWidth = "unset";
              ele = ele.parentElement;
              ele.style.width = this.width;
              ele.style.margin = this.margin;
            }
            // 文章信息
            ele = document.querySelector(
              "div.QuestionHeader div.QuestionHeader-content"
            );
            if (ele) {
              ele.style.padding = "unset";
              ele.style.width = "unset";
              ele = ele.firstChild;
              ele.style.width = "unset";
            }

            // 主页
            ele = document.querySelector(".Topstory-mainColumn");
            if (ele) {
              ele.style.width = this.width;
              ele.style.margin = this.margin;
            }
          }
        },
        // 知乎搜索
        () => {
          if (this.answer() == ZhiHu.answer) {
            document.querySelector("div.css-knqde")?.remove();
            // 主体内容
            let ele = document.querySelector("div#SearchMain");
            if (ele) {
              ele.style.width = this.width;
              ele.style.margin = this.margin;
            }
            // 顶部导航
            ele = document.querySelector("div.SearchTab-bottomShadow");
            if (ele) {
              ele.style.width = this.width;
              ele.style.margin = this.margin;
            }
          }
        },
        // 知乎历史浏览
        () => {
          if (this.answer() == ZhiHu.answer) {
            // 主体内容
            let ele = document.querySelector("main");
            if (ele) {
              ele.style.backgroundColor = this.backgroundColor;
            }
            // 主体子内容
            ele = document.querySelector("div.css-9511cm");
            if (ele) {
              ele.style.width = this.width;
              ele.style.margin = "unset";
            }
            // 主体子内容
            ele = document.querySelector("div.css-8gasvh");
            if (ele) {
              ele.style.padding = "unset";
              ele.style.margin = this.margin;
            }
          }
        },
        () => {
          // 关闭返回顶部
          document.querySelector('div[role="complementary"]')?.remove();
          // 关闭弹窗
          document
            .querySelector("div.Modal-wrapper")
            ?.parentElement.parentElement.parentElement.remove();
          // 主体内容滑动
          const body = document.querySelector("html");
          body.style.overflowY = "scroll";
          body.style.height = "100vh";
          body.style.margin = "unset";
        },
      ];

      // 添加监听事件
      this.addListentEvent = [
        {
          obj: document,
          whiteName: "WHITE_DOMContentLoaded",
          func: () => {
            if (this.answer() == ZhiHu.article) {
              document.querySelector("#root").style.marginTop = "30px";
              const ele = document.querySelector(".Post-Row-Content-left");
              ele.style.width = "1000px";
              ele.style.margin = "auto";
              ele.style.borderRadius = "10px";
            }
            document.body.style.backgroundColor = this.backgroundColor;

            this.CreateBackTop();
          },
        },
      ];
    }
    answer() {
      // 文章
      let t;
      if (this.host === "zhuanlan.zhihu.com") {
        t = ZhiHu.article;
      } else {
        // www.zhihu.com
        t = ZhiHu.answer;
      }
      return t;
    }
    DomTree() {
      /** 针对dom树进行修改 */
      new MutationObserver((events, observer) => {
        events.forEach((e) => {
          e.addedNodes.forEach((target) => {
            if (target && target.nodeType == 1) {
              this.domTreeChange.forEach((func) => {
                try {
                  func();
                } catch (error) {
                  console.error(error);
                }
              });
            }
          });
        });
      }).observe(document.documentElement, { childList: true, subtree: true });
    }
    WindowEvent() {
      // 拦截window事件
      const whiteList = this.addListentEvent.map((item) => item.whiteName);
      window._addEventListener = window.addEventListener;
      Object.defineProperty(window, "addEventListener", {
        value: (name, func) => {
          this.log && console.log(`window 事件名：${name}\t函数：${func}`);
          if (this.winBlackEvenName.includes(name)) {
            this.log && console.log(`取消监听${name}事件！`);
            return;
          }
          if (whiteList.includes(name)) {
            window._addEventListener(name.split("_").at(1), func);
            return;
          }
          window._addEventListener(name, func);
        },
      });
    }
    DocumentEvent() {
      // 拦截document事件
      const whiteList = this.addListentEvent.map((item) => item.whiteName);
      document._addEventListener = document.addEventListener;
      Object.defineProperty(document, "addEventListener", {
        value: (name, func) => {
          this.log && console.log(`document 事件名：${name}\t函数：${func}`);
          if (this.docBlackEvenName.includes(name)) {
            this.log && console.log(`取消监听${name}事件！`);
            return;
          }
          if (whiteList.includes(name)) {
            document._addEventListener(name.split("_").at(1), func);
            return;
          }
          document._addEventListener(name, func);
        },
      });
    }
    AddListenEvent() {
      // 添加监听事件
      this.addListentEvent.forEach((item) => {
        item.obj.addEventListener(item.whiteName, item.func);
      });
    }
    CreateBackTop() {
      const div = document.createElement("div");
      const btnTop = document.createElement("button");
      const btnButtom = document.createElement("button");
      const backTop = `<svg t="1749719721259" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1052" width="32" height="32"><path d="M484.538555 297.401434 78.508052 703.407378c-11.784394 11.813047-11.784394 30.912034 0 42.724057 11.81714 11.81407 30.94171 11.81407 42.757826 0l386.211109-386.181433 379.830795 379.806235c11.453866 11.482519 30.039153 11.482519 41.491996 0 11.511171-11.453866 11.511171-30.039153 0-41.516556L534.372543 303.776631c-1.543146-1.539053-3.417843-2.296299-5.200442-3.412726-0.691755-0.935302-1.055029-2.085498-1.933025-2.962471-11.784394-11.784394-30.912034-11.784394-42.695405 0L484.538555 297.401434zM484.538555 297.401434" fill="#1afa29" p-id="1053"></path></svg>`;
      const backButtom = `<svg t="1749719761870" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1082" width="32" height="32"><path d="M539.461445 726.598566L945.491948 320.592622c11.784394-11.813047 11.784394-30.912034 0-42.724057-11.81714-11.81407-30.94171-11.81407-42.757826 0l-386.211109 386.181433-379.83079499-379.806235c-11.453866-11.482519-30.039153-11.482519-41.49199601 0-11.511171 11.453866-11.511171 30.039153 0 41.516556L489.627457 720.223369c1.543146 1.539053 3.417843 2.296299 5.200442 3.412726 0.691755 0.935302 1.055029 2.085498 1.933025 2.962471 11.784394 11.784394 30.912034 11.784394 42.695405 0L539.461445 726.598566zM539.461445 726.598566" fill="#1afa29" p-id="1083"></path></svg>`;
      btnTop.innerHTML = backTop;
      btnButtom.innerHTML = backButtom;

      btnTop.style.backgroundColor = "transparent";
      btnTop.style.transition = "transform 0.3s ease";
      btnTop.onmouseover = () => {
        btnTop.style.transform = "scale(1.1)";
      };
      btnTop.onmouseout = () => {
        btnTop.style.transform = "scale(1)";
      };

      btnButtom.style.backgroundColor = "transparent";
      btnButtom.style.transition = "transform 0.3s ease";
      btnButtom.onmouseover = () => {
        btnButtom.style.transform = "scale(1.1)";
      };
      btnButtom.onmouseout = () => {
        btnButtom.style.transform = "scale(1)";
      };

      btnTop.addEventListener("click", () => {
        window.scrollTo({
          top: 0,
          behavior: "smooth",
        });
      });
      btnButtom.addEventListener("click", () => {
        window.scrollTo({
          top: document.body.scrollHeight,
          behavior: "smooth",
        });
      });

      div.style.zIndex = 9999999;
      div.style.position = "fixed";
      div.style.right = "150px";
      div.style.bottom = "50px";
      div.style.textAlign = "center";
      div.style.cursor = "pointer";
      div.style.display = "flex";
      div.style.flexDirection = "column";

      div.appendChild(btnTop);
      div.appendChild(btnButtom);
      document.body.appendChild(div);
    }

    main() {
      this.DomTree();
      this.WindowEvent();
      this.DocumentEvent();
      this.AddListenEvent();
    }
  }
  new Wonderful().main();
})();
